import QtQuick
import QtQuick.Controls
import "components"

ApplicationWindow {
    visible: true
    width: 600
    height: 500
    
    property date currentDate: new Date()
    property int currentMonth: currentDate.getMonth()
    property int currentYear: currentDate.getFullYear()
    
    Column {
        anchors.fill: parent
        anchors.margins: 20
        spacing: 10
        
        // 月份导航
        Row {
            spacing: 10
            anchors.horizontalCenter: parent.horizontalCenter
            
            Button {
                text: "<"
                onClicked: {
                    if (currentMonth === 0) {
                        currentMonth = 11
                        currentYear--
                    } else {
                        currentMonth--
                    }
                }
            }
            
            Label {
                text: Qt.locale().standaloneMonthName(currentMonth) + " " + currentYear
                font.pixelSize: 18
                font.bold: true
            }
            
            Button {
                text: ">"
                onClicked: {
                    if (currentMonth === 11) {
                        currentMonth = 0
                        currentYear++
                    } else {
                        currentMonth++
                    }
                }
            }
        }
        
        // 日历主体
        Row {
            spacing: 10
            
            // 周数列
            WeekNumberColumnComponent {
                width: 40
                y: 50
                height: monthGrid.height
                month: currentMonth
                year: currentYear
                
                delegate: Rectangle {
                    implicitWidth: 40
                    implicitHeight: 40
                    color: "transparent"
                    
                    Text {
                        text: model.weekNumber
                        anchors.centerIn: parent
                        color: "gray"
                    }
                }
            }
            
            Column {
                spacing: 5
                
                // 星期标题行
                DayOfWeekRowComponent {
                    width: monthGrid.width
                    height: 40
                    
                    delegate: Rectangle {
                        implicitWidth: 40
                        implicitHeight: 40
                        color: "#f0f0f0"
                        
                        Text {
                            text: model.shortName
                            anchors.centerIn: parent
                            font.bold: true
                        }
                    }
                }
                
                // 月历网格
                MonthGridComponent {
                    id: monthGrid
                    month: currentMonth
                    year: currentYear
                    
                    delegate: Rectangle {
                        implicitWidth: 40
                        implicitHeight: 40
                        color: {
                            if (!model.day)
                                return "transparent"
                            if (model.today)
                                return "#e6f3ff"
                            if (model.month === monthGrid.month)
                                return "white"
                            return "#f9f9f9"
                        }
                        border.color: "#e0e0e0"
                        
                        Text {
                            text: model.day
                            anchors.centerIn: parent
                            color: {
                                if (!model.day)
                                    return "transparent"
                                if (model.month !== monthGrid.month)
                                    return "gray"
                                return "black"
                            }
                            font.bold: model.today
                        }
                        
                        MouseArea {
                            anchors.fill: parent
                            enabled: model.day && model.month === monthGrid.month
                            onClicked: console.log("选择的日期:", model.date.toLocaleDateString())
                        }
                    }
                }
            }
        }
    }
}
